/*
 * StartAllAction.java
 *
 * Copyright 2007, 2008 Tiberiumlabs
 *
 * This file is part of Tiberiumlabs Lailaps.
 *
 * Tiberiumlabs Lailaps is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 3 of the License, or
 * (at your option) any later version.
 *
 * Tiberiumlabs Lailaps is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package org.tiberiumlabs.lailaps.actions.list;

import java.awt.event.ActionEvent;
import org.tiberiumlabs.lailaps.actions.AbstractResourcedAction;
import org.tiberiumlabs.lailaps.list.DownloadItem;
import org.tiberiumlabs.lailaps.list.DownloadsListModel;
import org.tiberiumlabs.lailaps.list.StatusConstants;
import org.tiberiumlabs.lailaps.resources.ImageLoader;
import org.tiberiumlabs.lailaps.threads.ThreadsMap;
import org.tiberiumlabs.lailaps.toolkit.StateManager;

/**
 *
 * @author <a href="mailto:ss.paranoid@gmail.com">Paranoid</a>
 */
public class StartAllAction extends AbstractResourcedAction implements StatusConstants {

    protected final ThreadsMap threadsMap = mediator.getThreadsMap();

    /** Creates a new instance of StartAllAction */
    public StartAllAction() {
        // TODO set other resource bundles
        putValue(NAME, bundle.getString("list.startall.name"));
        putValue(SHORT_DESCRIPTION, bundle.getString("list.startall.short"));
        putValue(LONG_DESCRIPTION, bundle.getString("list.startall.long"));
        putValue(LARGE_ICON_KEY, ImageLoader.getStartImage());
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        DownloadsListModel model = mediator.getDownloadsListModel();

        int stopped = 0;

        int count = model.getRowCount();
        for (int i = 0; i < count; i++) {
            DownloadItem item = model.getItem(i);
            switch (item.getStatus()) {
                case ERROR:
                case STOPPED:
                    {
                        // count stopped and countinue to execute to do same actions as other states
                        stopped++;
                    }
                case PAUSED:
                    {
                        threadsMap.create(item).start(i);
                    }
                default:
                    continue;
            }
        }

        StateManager stateManager = mediator.getStateManager();
        stateManager.change(StateManager.STOPPED, StateManager.WORKING, stopped);
    }
}